package com.mercury.rabbitmq.config.dead;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 死信队列配置类
 *
 * @author mercury
 */
@Configuration
@ConditionalOnProperty(
        name = {"spring.rabbitmq.enabled"},
        havingValue = "true"
)
public class DeadBuilderQueueConfig {



    /**
     * 声明死信队列
     *
     * @return 死信队列
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DeadBuilder.DEAD_LETTER_QUEUE_NAME);
    }

    /**
     * 声明死信交换机
     *
     * @return 死信交换机
     */
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DeadBuilder.DEAD_LETTER_EXCHANGE);
    }


    /**
     * 绑定死信队列和死信交换机
     *
     * @param queue    队列
     * @param exchange 交换机
     * @return 绑定
     */
    @Bean
    public Binding deadLetterBinding(@Qualifier("deadLetterQueue") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DeadBuilder.DEAD_LETTER_QUEUE_ROUTING_KEY);
    }


}
